import {
  myRequesy
} from '../../request/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myObj: {},
    avatarUrl: "",
    Uid: "",
    feedbackData: {},
    is_admin: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getfeedbackData()


  },


  // 点击授权
  headleLogin() {
    if (!wx.getStorageSync('userInfo')) {
      this.getLogin()
    }
  },

  // 登录授权
  getLogin() {
    let that = this
    wx.getUserProfile({
      desc: '获取您的用户名和头像',
      success(res1) {
        wx.login({
          success(res2) {
            let userData = {
              code: res2.code,
              nickName: res1.userInfo.nickName,
              avatarUrl: res1.userInfo.avatarUrl
            }
            myRequesy({
              url: 'Login',
              data: userData,
              method: "POST",
            }).then(res => {
              if (res.code === 0) {
                wx.setStorage({
                  data: res.data,
                  key: 'userInfo',
                  success() {
                    that.onShow()
                  }
                })
              } else if (res.code === 2001 || res.code === 2002 || res.code === 2003) {
                wx.removeStorage('userInfo')
              }
            })
          }
        })
      }
    })
  },

  // 用户数据
  getMyData() {
    myRequesy({
      url: 'get-my-count'
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          myObj: res.data
        })
      }
    })
  },

  // 跳转
  headleParticulars(e) {
    if (wx.getStorageSync('userInfo')) {
      let {url} = e.currentTarget.dataset
      wx.navigateTo({ url})
      return 
    } 
    this.getLogin()

  },


  // 获取反馈数据
  getfeedbackData() {
    myRequesy({
      url: 'get-feedback'
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          feedbackData: res.data
        })
      }
    })
  },


  // 联系客服或商务合作
  headleRelation(e) {
    let {
      state
    } = e.currentTarget.dataset
    let {
      kf_img,
      sw_img
    } = this.data.feedbackData
    wx.previewImage({
      urls: [state === 1 ? kf_img : sw_img],
    })
  },

  // 跳转webView
  headleWebView() {
    wx.navigateTo({
      url: '/pages/webView/webView?url=' + encodeURIComponent(this.data.feedbackData.problem),
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    if (wx.getStorageSync('userInfo')) {
      this.getMyData()
      this.setData({
        userInfo: wx.getStorageSync('userInfo'),
        is_admin: wx.getStorageSync('userInfo').is_super
      })
    }

    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})